package com.niiwoo.civet.trade.service.local.kaola;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.niiwoo.civet.trade.dto.response.kaola.KaoLaLiCaiReceiveDTO;
import com.niiwoo.civet.trade.enums.kaola.ErrorCodeConstants;
import com.niiwoo.civet.trade.enums.kaola.KaoLaLiCaiReceiveEnum;
import com.niiwoo.civet.trade.property.KaoLaLiCaiProperties;
import com.niiwoo.civet.trade.utils.koala.AES;
import com.niiwoo.tripod.base.exception.DecryptException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author daizhiyue
 * @description 考拉理财异步通知
 * @date 2017-12-25
 */
@Service
@Slf4j
@EnableConfigurationProperties(KaoLaLiCaiProperties.class)
public class KaoLaLiCaiReceiveFacadeService {


    @Autowired
    private KaoLaLiCaiProperties kaoLaLiCaiProperties;

    @Autowired
    private KaoLaLiCaiReceiveServiceFactory kaoLaLiCaiReceiveServiceFactory;

    public KaoLaLiCaiReceiveDTO processService(Map<String, Object> requestMap, String path) {
        String platform_id = null;
        String data = null;
        String decryptDataStr = null;
        JSONObject dataObj = null;
        KaoLaLiCaiReceiveEnum kaoLaLiCaiReceiveEnum = null;
        //参数非空检查
        if (requestMap == null) {
            log.error("KaoLaLiCaiReceiveFacade param is null");
            return new KaoLaLiCaiReceiveDTO(ErrorCodeConstants.PARAM_ERROR, ErrorCodeConstants.PARAM_ERROR_MSG);
        }

        //商户号判断
        platform_id = (String) requestMap.get("platform_id");
        if (platform_id == null) {
            log.error("KaoLaLiCaiReceiveFacade platform_id is null");
            return new KaoLaLiCaiReceiveDTO(ErrorCodeConstants.PARAM_ERROR, ErrorCodeConstants.PARAM_ERROR_MSG);
        }

        //业务参数判断
        data = (String) requestMap.get("data");
        if (data == null) {
            log.error("KaoLaLiCaiReceiveFacade data is null");
            return new KaoLaLiCaiReceiveDTO(ErrorCodeConstants.PARAM_ERROR, ErrorCodeConstants.PARAM_ERROR_MSG);
        }

        //商户号检查是否一致
        if (!kaoLaLiCaiProperties.getPlatformId().equals(platform_id)) {
            log.error("KaoLaLiCaiReceiveFacade invalid platformId");
            return new KaoLaLiCaiReceiveDTO(ErrorCodeConstants.PARAM_ERROR, ErrorCodeConstants.PARAM_ERROR_MSG);
        }

        //解密报文
        try {
            decryptDataStr = AES.Decrypt(data, kaoLaLiCaiProperties.getAesKey(), kaoLaLiCaiProperties.getAesIv());
        } catch (Exception e) {
            log.error("KaoLaLiCaiReceiveFacade decryptDataStr exception", e);
            throw new DecryptException("报文解密失败");
        }
        dataObj = JSON.parseObject(decryptDataStr);

        kaoLaLiCaiReceiveEnum = KaoLaLiCaiReceiveEnum.getEnumByPath(path);
        if (kaoLaLiCaiReceiveEnum == null) {
            log.error("KaoLaLiCaiReceiveFacade invalid path");
            return new KaoLaLiCaiReceiveDTO(ErrorCodeConstants.SERVICE_METHOD_NOT_EXIST, ErrorCodeConstants.SERVICE_METHOD_NOT_EXIST_MSG);
        }

        return this.doService(dataObj, kaoLaLiCaiReceiveEnum);
    }


    private KaoLaLiCaiReceiveDTO doService(Map<String, Object> requestMap, KaoLaLiCaiReceiveEnum receiveEnum) {
        KaoLaLiCaiReceiveService kaoLaLiCaiReceiveService
                = kaoLaLiCaiReceiveServiceFactory.getServiceByEnum(receiveEnum);

        KaoLaLiCaiReceiveDTO baseResponse = kaoLaLiCaiReceiveService.doService(requestMap);
        return baseResponse;
    }
}

